package com.zhongyouyun.admin.service.imp;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.model.RoleMenuModel;
import com.zhongyouyun.admin.mapper.RoleMenuMapper;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.model.UserRoleModel;
import com.zhongyouyun.admin.pojo.MenuPojo;
import com.zhongyouyun.admin.pojo.RoleMenuPojo;
import com.zhongyouyun.admin.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyouyun.admin.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色资源中间表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuModel> implements RoleMenuService {
    @Autowired
    private UserRoleService userRoleService;
    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<RoleMenuModel> selectPage(Page<RoleMenuModel> page, QueryWrapper<RoleMenuModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<RoleMenuModel> selectList(QueryWrapper<RoleMenuModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Tree<String>> selectRoleMenuList(UserModel userModel) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userModel.getId());
        List<UserRoleModel> userRoleModelList= userRoleService.list(queryWrapper);
        List<MenuModel> menuModelList = this.baseMapper.selectMenuList(userModel);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> list = TreeUtil.build(menuModelList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getPId().toString());
                    tree.putExtra("name", treeNode.getName());
                    tree.putExtra("url", treeNode.getUrl());
                    tree.putExtra("state", treeNode.getState());
                    tree.putExtra("icon", treeNode.getIcon());
                    tree.putExtra("idDel", treeNode.getIdDel());
                });
        return list;
    }

    @Override
    public List<RoleMenuPojo> findPermissions(UserModel userModel) {
        return this.baseMapper.findPermissions(userModel);
    }
}
